import { users } from '../../server/database/schema';
import {eq} from 'drizzle-orm'

export default defineEventHandler(async (event) => {
  const session = await getUserSession(event)

  const { nickname } = await readBody(event)
  const db = useDrizzle()

  await db.update(users).set({ nickname }).where(eq(users.email, session.user?.email || ''))

  // 重新查询用户信息
  const updatedUser = await db.select()
    .from(users)
    .where(eq(users.email, session.user?.email || ''))
    .limit(1)

  // 更新 session
  await setUserSession(event, { user: updatedUser[0] })


  return {success: true}
})
